#region File Description
//-----------------------------------------------------------------------------
// PictureScreen.cs, derived from MessageBoxScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace TankWars
{
    /// <summary>
    /// A popup picture screen, used to display a single image (i.e. controller configuration)
    /// </summary>
    class PictureScreen : GameScreen
    {
        #region Fields

        Texture2D gradientTexture;      // Background gradient
        Texture2D foregroundImage;

        #endregion

        #region Events

        public event EventHandler<EventArgs> Accepted;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public PictureScreen()
        {
            IsPopup = true;

            TransitionOnTime = TimeSpan.FromSeconds(0.2);
            TransitionOffTime = TimeSpan.FromSeconds(0.2);
        }


        /// <summary>
        /// Loads graphics content for this screen. This uses the shared ContentManager
        /// provided by the Game class, so the content will remain loaded forever.
        /// Whenever a subsequent PictureScreen tries to load this same content,
        /// it will just get back another reference to the already loaded data.
        /// </summary>
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            gradientTexture = content.Load<Texture2D>("gradient");
            foregroundImage = content.Load<Texture2D>("Backgrounds\\TankControls360");
        }


        #endregion
        
        /*
        /// <summary>
        /// Updates screen by sleeping for a few seconds and then removing itself
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            Thread.Sleep(3000);

            ExitScreen();
        }
        */
        
        #region Handle Input


        /// <summary>
        /// Responds to user input, accepting or cancelling the message box.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            if (input.MenuSelect || input.MenuClick || input.MenuCancel)
            {
                // Raise the accepted event, then exit the message box.
                if (Accepted != null)
                    Accepted(this, EventArgs.Empty);

                ExitScreen();
            }
        }


        #endregion
        

        #region Draw


        /// <summary>
        /// Draws the message box.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            
            // Darken down any other screens that were drawn beneath the popup.
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);

            // Center the image in the viewport.
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
            Vector2 imageSize = new Vector2(foregroundImage.Width, foregroundImage.Height);
            Vector2 imagePosition = (viewportSize - imageSize) / 2;

            // The background includes a border somewhat larger than the text itself.
            const int hPad = 32;
            const int vPad = 16;

            Rectangle backgroundRectangle = new Rectangle((int)imagePosition.X - hPad,
                                                          (int)imagePosition.Y - vPad,
                                                          (int)imageSize.X + hPad * 2,
                                                          (int)imageSize.Y + vPad * 2);

            // Fade the popup alpha during transitions.
            Color color = new Color(255, 255, 255, TransitionAlpha);

            spriteBatch.Begin();

            // Draw the background rectangle.
            spriteBatch.Draw(gradientTexture, backgroundRectangle, color);

            // Draw the picture.
            spriteBatch.Draw(foregroundImage, imagePosition, Color.White);

            spriteBatch.End();
        }


        #endregion
    }
}
